﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class F_sltj : Form
    {
        public F_sltj()
        {
            InitializeComponent();
            conn = new SqlConnection();
            conn.ConnectionString =SQL.getCon;
            conn.Open();
            da = new SqlDataAdapter("select mc as 名称,bmid from bmb", conn);
            DataSet ds1 = new DataSet();
            da.Fill(ds1, "部门表");
            conn.Close();
            comboBox1.DataSource = ds1.Tables["部门表"];
            comboBox1.DisplayMember = "名称";
            comboBox1.ValueMember = "bmid";
            showmax();
            label2.Text = "A" + ma1;
        }

        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;
        DataSet ds;
        SqlDataReader dr;
        public int ma1;
        public string ypbm;
        public string strno;
        int i = 1;
        string xh1;
        public int xh;

        void showmax()
        {
            conn = new SqlConnection();
            conn.ConnectionString =SQL.getCon;
            conn.Open();
            com = new SqlCommand();
            com.CommandText = "select max(id) as id from ypsfjl";
            com.Connection = conn;
            dr = com.ExecuteReader();
            dr.Read();
            string s1;
            s1 = dr["id"].ToString();
            if (s1 == "") { ma1 = 0; }
            else
                ma1 = int.Parse(s1);
            conn.Close();
        }

        void insert()
        {
            conn = new SqlConnection();
            conn.ConnectionString =SQL.getCon;
            com = new SqlCommand();
            com.Connection = conn;
            conn.Open();
            com.CommandText = "select ypid from ypgg where bm=" + ypbm;
            dr = com.ExecuteReader();
            dr.Read();
            string yid;
            yid = dr["ypid"].ToString();
            conn.Close();
            conn.Open();
            com.CommandText = "insert into ypsfjl(id,jlzt,dj,no,xh,kfid,rcxs,ypid,pc,txsl,zy,tzr,tzrq) values (@id,@jlzt,@dj,@no,@xh,@kfid,@rcxs,@ypid,@pc,@txsl,@zy,@tzr,@tzrq)";
            SqlParameter id = new SqlParameter("id", SqlDbType.Int, 10);
            id.Value = ma1 + 1;
            com.Parameters.Add(id);
            SqlParameter jlzt = new SqlParameter("jlzt", SqlDbType.Int, 10);
            jlzt.Value = "1";
            com.Parameters.Add(jlzt);
            SqlParameter dj = new SqlParameter("dj", SqlDbType.Int, 10);
            dj.Value = "2";
            com.Parameters.Add(dj);
            SqlParameter no1 = new SqlParameter("no", SqlDbType.VarChar, 20);
            no1.Value = label2.Text;
            com.Parameters.Add(no1);
            SqlParameter xh = new SqlParameter("xh", SqlDbType.Int, 10);
            xh.Value = i;
            com.Parameters.Add(xh);
            SqlParameter kfid = new SqlParameter("kfid", SqlDbType.Int, 10);
            kfid.Value = comboBox1.SelectedValue.ToString();
            com.Parameters.Add(kfid);
            SqlParameter rcxs = new SqlParameter("rcxs", SqlDbType.Int, 10);
            rcxs.Value = "1";
            com.Parameters.Add(rcxs);
            SqlParameter ypid = new SqlParameter("ypid", SqlDbType.Int, 10);
            ypid.Value = yid;
            com.Parameters.Add(ypid);
            SqlParameter pc = new SqlParameter("pc", SqlDbType.NVarChar, 20);
            pc.Value = textBox11.Text;
            com.Parameters.Add(pc);
            SqlParameter txsl = new SqlParameter("txsl", SqlDbType.Int, 10);
            txsl.Value = textBox3.Text;
            com.Parameters.Add(txsl);
            SqlParameter zy = new SqlParameter("zy", SqlDbType.NVarChar, 50);
            zy.Value = textBox9.Text;
            com.Parameters.Add(zy);
            SqlParameter tzr = new SqlParameter("tzr", SqlDbType.NVarChar, 20);
            tzr.Value = textBox4.Text;
            com.Parameters.Add(tzr);
            SqlParameter tzrq = new SqlParameter("tzrq", SqlDbType.DateTime, 20);
            tzrq.Value = dateTimePicker1.Text;
            com.Parameters.Add(tzrq);
            com.ExecuteNonQuery();
            conn.Close();
        }

        void showfil()
        {
            conn = new SqlConnection();
            conn.ConnectionString =SQL.getCon;
            com = new SqlCommand();
            com.Connection = conn;
            com.CommandText = "select xh,jlzt,kfid,rcxs,mc,pc,txsl,zy,tzr,tzrq from ypsfjl,ypgg where ypsfjl.ypid=ypgg.ypid and no='" + label2.Text + "'";
            da = new SqlDataAdapter();
            da.SelectCommand = com;
            ds = new DataSet();
            da.Fill(ds, "ypsfjl");
            dataGridView1.DataSource = ds.Tables["ypsfjl"];
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            conn = new SqlConnection();
            conn.ConnectionString =SQL.getCon;
            com = new SqlCommand();
            com.Connection = conn;
            com.CommandText = "select bm as 编码,mc as 名称,gg as 规格,cd as 产地,zdxq as 最大效期 from ypgg where bm like '" + textBox2.Text + "%'";
            da = new SqlDataAdapter();
            ds = new DataSet();
            da.SelectCommand = com;
            da.Fill(ds, "药品规格");
            dataGridView2.DataSource = ds.Tables["药品规格"];
            dataGridView2.Visible = true;
        }

        private void dataGridView2_Click(object sender, EventArgs e)
        {
            ypbm = dataGridView2[0, dataGridView2.CurrentCell.RowIndex].Value.ToString();
            textBox2.Text = dataGridView2[1, dataGridView2.CurrentCell.RowIndex].Value.ToString();
            label22.Text = dataGridView2[2, dataGridView2.CurrentCell.RowIndex].Value.ToString();
            label23.Text = dataGridView2[3, dataGridView2.CurrentCell.RowIndex].Value.ToString();
            label24.Text = dataGridView2[4, dataGridView2.CurrentCell.RowIndex].Value.ToString();
        }

        private void dataGridView2_DoubleClick(object sender, EventArgs e)
        {
            dataGridView2.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showmax();
            ma1 = ma1 + 1;
            insert();
            i = i + 1;
            showfil();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (xh1 == null) { MessageBox.Show("请点击需要删除的药品"); }
            else
            {
                conn = new SqlConnection();
                conn.ConnectionString =SQL.getCon;
                com = new SqlCommand();
                com.Connection = conn;
                conn.Open();
                com.CommandText = "delete ypsfjl where xh=" + xh + " and no='" + label2.Text + "'";
                com.ExecuteNonQuery();
                conn.Close();
                conn.Open();
                com.CommandText = "Update ypsfjl set xh=xh-1 where xh>" + xh + " and no='" + label2.Text + "'";
                com.ExecuteNonQuery();
                conn.Close();
                showfil();
            };
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            xh1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            xh = int.Parse(xh1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出不保存吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                try
                {
                    conn = new SqlConnection();
                    conn.ConnectionString =SQL.getCon;
                    com = new SqlCommand();
                    com.CommandText = "delete from ypsfjl where no='" + label2.Text + "'";
                    com.Connection = conn;
                    conn.Open();
                    com.ExecuteNonQuery();
                    conn.Close();
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

    }
}